#include "reg51.h"
#include "OP_EEPROM.h"
#include "intrins.h"

sfr IAP_DATA = 0xC2;
sfr IAP_ADDRH = 0xC3;
sfr IAP_ADDRL = 0xC4;
sfr IAP_CMD = 0xC5;
sfr IAP_TRIG = 0xC6;
sfr IAP_CONTR = 0xC7;
sfr IAP_TPS = 0xF5;

void IAP_Idle(){
	IAP_CONTR = 0; // 关闭IAP功能
	IAP_CMD = 0;
	IAP_TRIG = 0;
	IAP_ADDRH = 0xff;
	IAP_ADDRL = 0;
}

void READ_EEPROM(unsigned int addr, unsigned int length, unsigned char *dat){
	unsigned int i;
	unsigned int addrReal;
	IAP_CONTR = 0x80; // 使能IAP
	IAP_TPS = 12; // 等待参数 12MHZ
	IAP_CMD = 1; // 读命令
	for (i=0; i<length; i++){
		addrReal = addr + i;
		IAP_ADDRL = addrReal;
		IAP_ADDRH = addrReal >> 8;
		IAP_TRIG = 0x5a;
		IAP_TRIG = 0xa5;
		_nop_();
		dat[i] = IAP_DATA;
	}
}